package com.yl.users.services.impl

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.entity.*
import com.yl.users.services.ICompanyRoleService
import com.yl.users.mapper.*
import com.yl.users.pojo.vo.CompanyPermissionsViewSelectVo
import com.yl.users.services.ICompanyUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

@Service
open class CompanyRoleServiceImpl(
	private val companyRoleMapper: CompanyRoleMapper,
	private val companyRolePermissionsMapper: CompanyRolePermissionsMapper,
	private val companyRoleTableFieldMapper: CompanyRoleTableFieldMapper
) : ICompanyRoleService {

	@Autowired
	private lateinit var iCompanyUserService: ICompanyUserService

	override fun getCompanyRole(roleId: Long): CompanyRole? {
		return companyRoleMapper.selectById(roleId)
	}

	override fun delAllPermToRole(roleId: Long) {
		companyRolePermissionsMapper.delete(Wrappers.query<CompanyRolePermissions?>().eq("role_id", roleId))
	}

	override fun addPermToRole(roleId: Long, permViesIds: List<Long>) {
		for (permId in permViesIds) {
			companyRolePermissionsMapper.insert(CompanyRolePermissions(roleId = roleId, permissionsViewId = permId))
		}
	}

	override fun update(companyRole: CompanyRole): ResponseBody {
		val role =
			companyRoleMapper.selectById(companyRole.id) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "角色未找到")

		if (companyRole.roleName.isEmpty()) {
			return returnError(ErrorCode.PARAM_EMPTY, "请输入角色名称")
		}
		role.roleName = companyRole.roleName
		companyRoleMapper.updateById(role)
		return returnSuccess()

	}

	override fun reBindRolePermissions(roleId: Long, permViesIds: List<Long>): ResponseBody {
		companyRolePermissionsMapper.delete(Wrappers.query<CompanyRolePermissions>().eq("role_id", roleId))
		addPermToRole(roleId, permViesIds)
		iCompanyUserService.cleanCacheByCompany(getContext().companyId)
		return returnSuccess()
	}

	override fun reBindRoleField(fields: List<Long>, roleId: Long) {
		companyRoleTableFieldMapper.delete(Wrappers.query<CompanyRoleTableField>().eq("role_id", roleId))
		for (id in fields) {
			val field = CompanyRoleTableField(
				id = 0L,
				tableFieldId = id,
				roleId = roleId
			)

			companyRoleTableFieldMapper.insert(field)
		}
		iCompanyUserService.cleanCacheByCompany(getContext().companyId)
	}

	override fun delRole(roleId: Long) {
		companyRoleMapper.deleteById(roleId)
		iCompanyUserService.cleanCacheByCompany(getContext().companyId)
	}

	override fun getRolePermIds(roleId: Long): List<Long> {
		return companyRolePermissionsMapper.selectRolePermIds(roleId)
	}

	override fun getRolePermList(roleId: Long, vo: CompanyPermissionsViewSelectVo): List<CompanyPermissionsView> {
		return companyRolePermissionsMapper.selectRolePermList(roleId, vo)
	}

	override fun getRoleFieldIds(roleId: Long): List<Long> {
		return companyRoleTableFieldMapper.getRoleFieldIds(roleId)
	}

	override fun getRoleFieldList(roleId: Long): List<CompanyPermissionsView> {
		return companyRoleTableFieldMapper.getRoleFieldList(roleId)
	}
}
